#include ActionView::Helpers::UrlHelper

class BookmarksController < ApplicationController
  before_filter :login_required #, :get_current_tags

  # GET /bookmarks
  # GET /bookmarks.xml
  def index
    @bookmarks = Bookmark.paginate_by_user_id current_user.id, :page => params[:page], :per_page=>Bookmark::PER_PAGE, :order => 'updated_at DESC'
    @title = "查看我的书签"
    @mytags = get_current_tags


    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @bookmarks }
    end
  end

  def tagged_with
    # TODO: get bookmarks just for current user
    @bookmarks = Bookmark.find_tagged_with(params[:tagname]).paginate
    @title = "查看标签为#{params[:tagname]}的书签"
    @mytags = get_current_tags
    render :action=>:index
   # @bookmarks = Bookmark.paginate_by_user_id current_user.id, :page => params[:page], :per_page=>Bookmark::PER_PAGE, :order => 'updated_at DESC'
  end

  # GET /bookmarks/1
  # GET /bookmarks/1.xml
  def show
    @bookmark = Bookmark.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @bookmark }
    end
  end

  # GET /bookmarks/new
  # GET /bookmarks/new.xml
  def new
    source_id = flash[:source_id] || params[:id]

    @bookmark = Bookmark.find_by_user_id_and_source_id(current_user.id, source_id)

    unless @bookmark.nil?
      render :action=>"edit", :id=>@bookmark.id
    else
      @bookmark = Bookmark.new
      @bookmark.source_id = source_id

      source = Source.find(@bookmark.source_id)
      unless source.nil?
        begin
          res = Net::HTTP.get_response(URI.parse(source.uri))
          if /<title>(.*?)<\/title>/i.match(res.body)
            @bookmark.title = $1
          end
        rescue
          @bookmark.title = "******"
        end

        @mytags = get_current_tags
        @myselecttags = get_current_tags(false)
        @poptags=source.tag_list
      end

      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @bookmark }
      end
    end
  end




  # GET /bookmarks/1/edit
  def edit
    @bookmark = Bookmark.find(params[:id])
    @mytags = get_current_tags
    @myselecttags = get_current_tags(false)
    @poptags=@bookmark.source.tag_list
  end

  # POST /bookmarks
  # POST /bookmarks.xml
  def create
    @bookmark = Bookmark.new(params[:bookmark])
    @bookmark.user_id = current_user.id

    respond_to do |format|
      if @bookmark.save
        flash[:notice] = 'Bookmark was successfully created.'
        format.html { redirect_to(:action=>:index) }
        format.xml  { render :xml => @bookmark, :status => :created, :location => @bookmark }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @bookmark.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /bookmarks/1
  # PUT /bookmarks/1.xml
  def update
    @bookmark = Bookmark.find(params[:id])
    @bookmark.user_id = current_user.id

    respond_to do |format|
      if @bookmark.update_attributes(params[:bookmark])
        flash[:notice] = 'Bookmark was successfully updated.'
        format.html { redirect_to(:action=>:index) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @bookmark.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /bookmarks/1
  # DELETE /bookmarks/1.xml
  def destroy
    @bookmark = Bookmark.find(params[:id])
    @bookmark.destroy

    respond_to do |format|
      format.html { redirect_to(bookmarks_url) }
      format.xml  { head :ok }
    end
  end

 # private
  
end
